package util;

import java.io.FileInputStream;
import java.sql.*;
import java.util.Properties;

/**
 * @author Administrator
 * @date 2024-11-04 17:54
 */
public class DBUtil {
    //封闭,不让其他类直接床啊金对象
     private DBUtil(){}
    private static Properties properties=new Properties();

     static {
         //加载文件信息
         try {
             //加载文件信息
             properties.load(new FileInputStream("day36/src/jdbc.properties"));
             //注册驱动
             Class<?> aClass = Class.forName(properties.getProperty("jdbc.driver"));

         } catch (Exception e) {
             throw new RuntimeException(e);
         }
     }
    /**
     * 获取连接
     */
    public static Connection getConnection(){
        Connection connection = null;
        try {
            // 获取连接
            connection = DriverManager.getConnection(properties.getProperty("jdbc.url"),properties.getProperty("jdbc.username"), properties.getProperty("jdbc.password"));

        }catch (Exception e){
            e.printStackTrace();
        }
        return connection;
    }
    /*
    释放支援
     */
    public static void release(Connection connection) throws SQLException {
        if (connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
    public static void release(Connection connection, PreparedStatement preparedStatement) throws SQLException {
        if (connection!=null){
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        connection.close();
    }

    public static void release(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) throws SQLException {
        if (connection!=null){
            try {
                resultSet.close();

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        release(connection,preparedStatement);
    }


}
